% "FGNV 2022 Table 1"
% based on codes from Jonathan Heathcote and Hitoshi Tsujiyama, 2021
clear; clc; close all; format long;
addpath './modules' './functions' './variables' './tools'

for index_case = 1:5    % 1 if benchmark, 2 HSV, 3 is HT,
                        % 4 use thinner Pareto tail (which equates HSV and affine welfare gains),
                        % 5 use thinner Pareto tails (which matches evidence from Toda et al.)
                        
    Parameters           % Parameter Setup    
    BaselineEconomy
    FirstBest
    SecondBest           % Mirrlees Second Best
    Ramsey               % Ramsey Taxation
    
    % Prepare Grid
    if (index_case == 1)        % Benchmark
        tau_min = -0.10; tau_max = tau_min + 0.01; T_min = 0.22; T_max = T_min + 0.01;
    elseif (index_case==2)      % HSV
        tau_min = -0.11; tau_max = tau_min + 0.01; T_min = 0.20; T_max = T_min + 0.01; 
    elseif (index_case==3)      % HT
        tau_min = 0.185; tau_max = tau_min + 0.01; T_min = 0.145; T_max = T_min + 0.01;
    elseif (index_case==4)      % HT thinner tail [1]
        tau_min = 0.155; tau_max = tau_min + 0.01; T_min = 0.16; T_max = T_min + 0.01;
    elseif (index_case==5)      % HT thinner tail [2]
        tau_min = 0.08; tau_max = tau_min + 0.01;  T_min = 0.195; T_max = T_min + 0.01;
    end
    tau_n = 11; tau_grid = linspace(tau_min,tau_max,tau_n)';
    T_n = 11; T_grid = linspace(T_min,T_max, T_n)';
    save T_grid T_grid
    save tau_grid tau_grid
    
    RamseyHSVAffine
    
    
    if (index_case == 1)        % Benchmark
        save Results_case1_benchmark sb r_hsv r_affine r_hsv_affine pai w pr
    elseif (index_case==2)      % HSV
        save Results_case2_HSV sb r_hsv r_affine r_hsv_affine pai w pr
    elseif (index_case==3)      % HT
        save Results_case3_HT sb r_hsv r_affine r_hsv_affine pai w pr
    elseif (index_case==4)      % HT thinner tail [1]
        save Results_case4_HT_Paretothinner sb r_hsv r_affine r_hsv_affine pai w pr
    elseif (index_case==5)      % HT thinner tail [2]
        save Results_case5_HT_Pareto_tode sb r_hsv r_affine r_hsv_affine pai w pr
    end

end

clc

for index_case = 1: 5 

    if (index_case == 1)        % Benchmark
        load Results_case1_benchmark
    elseif (index_case==2)      % HSV
        load Results_case2_HSV 
    elseif (index_case==3)      % HT
        load Results_case3_HT 
    elseif (index_case==4)      % HT thinner tail [1]
        load Results_case4_HT_Paretothinner 
    elseif (index_case==5)      % HT thinner tail [2]
        load Results_case5_HT_Pareto_tode 
    end

    display(['*** CASE       = ',num2str(index_case)])

    disp('*** Calib ***')
    display(['v_c       = ',num2str(pr.vc  )])
    display(['lambda_c  = ',num2str(pr.pareto/(1-pr.tau))])
    display(['phi       = ',num2str(pr.sig)])
    display(['g         = ',num2str(pr.g)])
    display(['Income share bottom quintile, top decile = ',num2str(quintiles(1)),', ',num2str(deciles(10))])
    
    disp('*** Welfare gains ***')
    display(['Mirrlees  = ',num2str(sb.WelfareGain)])
    display(['HSV + T   = ',num2str(r_hsv_affine.WelfareGain)])
    display(['HSV       = ',num2str(r_hsv.WelfareGain)])
    display(['Affine    = ',num2str(r_affine.WelfareGain)])

end 
